package com.lxh.lxhcrm.model.service;

import com.lxh.lxhcrm.model.dao.customer.BehaviorDao;
import com.lxh.lxhcrm.model.dao.customer.BehaviorRecordDao;
import com.lxh.lxhcrm.model.dao.preSale.ActRecordDao;
import com.lxh.lxhcrm.model.dao.preSale.ActTypeDao;
import com.lxh.lxhcrm.model.dao.preSale.DZanDao;
import com.lxh.lxhcrm.model.mappers.preSale.ActBehaviorMapper;
import com.lxh.lxhcrm.model.mappers.preSale.ActRecordMapper;
import com.lxh.lxhcrm.model.pojos.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 开发团队 ：lxh
 * 开发人员 ：罗晓
 * 开发时间 ：2020/2/29 10:35
 * 类的名称 ：ActRecordService
 * 类的描述 ：售前模块员工行动记录的service类，所有业务逻辑都在此
 * @author 罗晓
 */
@Service
@Transactional
public class ActRecordService {
    @Resource
    ActRecordMapper am;
    @Resource
    ActTypeDao att;
    @Resource
    ActRecordDao ard;
    @Resource
    ActBehaviorMapper abm;
    @Resource
    BehaviorDao bd;
    @Resource
    BehaviorRecordDao bereDao;
    @Resource
    CustomerService cs;
    @Resource
    DZanDao dd;


    /**找到所有的行动记录*/
    public List<ActionRecord> findAllActRecord(Integer type, String search, Integer value, Date startDate, Date endDate){
        return am.findAllActRecord(type,search,value,startDate,endDate);
    }
    /**找到所有的行动类*/
    public List<ActionType> getactType() {
        return att.findAll();
    }
    /** 添加和修改的方法*/
    public void addActRecord(ActionRecord actionRecord) {
        //先获取老的行动记录
//        List<Integer> listId = new ArrayList();
//        List<Map<String,Object>> listMap = null;
//        Integer item = 0;
        //先保存行动记录
        ActionRecord act = ard.save(actionRecord);
        if(actionRecord.getActionRecordId() != 0){
            //根据这个actionRecordId，删除这个list里的所有值
            abm.deleteBeha(actionRecord.getActionRecordId());
        }

        actionRecord.getBehaviorRecordList().forEach(beh -> {
            // 比对以前的机会中现在没有的：有的就剔除，剩下的就是没有的了
            beh.setActionRecord(actionRecord);
            bereDao.save(beh);
        });
        //获取客户数据
        cs.stageChange1(actionRecord.getCustomer());
    }
    /**找到所有的行动记录*/
    public List<ActionRecord> findActBeRecord(Integer customerId){
        return abm.findActBeRecord(customerId);
    }

    public List<Behavior> getBehaviorType() {
        return (List<Behavior>) bd.findAll();
    }

    public void addbehavior(BehaviorRecord behaviorRecord) {
        bereDao.save(behaviorRecord);
    }

    /**
     * 点赞引发的事件
     * @param empId
     * @param actionRecordId
     */
    public void giveLike(Integer empId, Integer actionRecordId) {
        List<DZan> dZan = dd.findByEmpIdAndActionRecordId(empId,actionRecordId);
        int change = 0;
        if(dZan.isEmpty()){
            change = 1;
            System.out.println(2);
            //没有记录就在点赞表里新增，在行动记录表里的zan字段加一
            am.saveLike(empId, actionRecordId);
            am.updateZan(change,actionRecordId);
        }else{
            change = -1;
            System.out.println(1);
            dd.deleteByEmpIdAndActionRecordId(empId,actionRecordId);
            am.updateZan(change,actionRecordId);
        }
    }

    /**
     * 删除行动记录
     * @param actionRecordId
     */
    public void deleteAction(Integer actionRecordId) {
        am.deleteByActId(actionRecordId);
        am.deleteAct(actionRecordId);
    }
}
